import {Component, OnInit} from '@angular/core';
import {Globals} from '../../../globals';
import {PageHelper} from '../../../utils/page-helper-model';
import {FormBuilder, FormGroup} from '@angular/forms';
import {UserInfoModel} from '../../../models/user-info.model';
import {RoleIdEnum} from '../../../enums/role-id.enum';
import {UserStatusEnum} from '../../../enums/user-status.enum';
import {BaseService} from '../../../shared/service/base.service';
import {ActivatedRoute, Router} from '@angular/router';
import {Consts} from '../../../config/consts';
import {ApiUrl} from '../../../config/api-url';
import {QualificationTypeEnum} from "../../../enums/qualification-type.enum";

@Component({
  selector: 'app-merchant-manage-list',
  templateUrl: './merchant-manage-list.component.html',
  styleUrls: ['./merchant-manage-list.component.less']
})
export class MerchantManageListComponent implements OnInit {
  isMobile = Globals.isMobile();
  listOfData: any;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  // 模态框表单
  formModel: FormGroup;
  // 显示模态框
  isVisible = false;

  feedback: any;
  merchantList: any = [];
  authenticationStatus = false;
  nzLoading = false;
  qualificationTypeEnum = {
    MAIN: QualificationTypeEnum.MAIN,
    COOPERATION: QualificationTypeEnum.COOPERATION,
  }

  // 机构
  agencyList: any[] = [];
  // 运营商
  userList: any[] = [];
  // 门店
  merchantStoreList: any[] = [];

  constructor(private baseService: BaseService, private fb: FormBuilder, private router: Router) {
    this.buildForm();
  }

  ngOnInit(): void {
    this.getAllAgencyOfName();
    this.getAllUserOfName();
    this.findAllMerchantStoreByUserInfo();
    this.findData();
  }

  buildForm() {
    this.formModel = this.fb.group({
      agencyId: ['', []],
      userId: ['', []],
      status: [null, []],
      outTradeNo: [null, []],
      merchantId: [null, []],
      certNo: [null, []],
      keyword: [null, []],
    });
  }

  submitForm() {
    this.pageHelper.currentPage = 1;
    this.findData();
  }

  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findData();
  }
  onPageSizeChange(pageSize: any){
    this.pageHelper.pageSize = pageSize;
    this.pageHelper.currentPage = 1;
    this.findData();
  }
  findData() {
    this.baseService.findListByMuchCondition(ApiUrl.MerchantUrl + '/findList', this.formModel.value, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.listOfData = data.data.list;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  editShow(data) {
    this.isVisible = true;
    if (data != null) {
      this.feedback = data;
    }
  }

  // 子组件向父组件传值
  setVisible(body) {
    this.isVisible = body.isVisible;
    this.findData();
  }

  skipMerchant(data: any) {
    this.router.navigateByUrl('/merchant?userId=' + data.userId,
      {skipLocationChange: true});
  }
  setVisibleAuthenticationStatus(body){
    this.authenticationStatus = body.isVisible;
  }

  showAuthenticationStatus(data: any){
    this.nzLoading = true;
    if (data != null) {
      this.feedback = data;
      this.baseService.post(ApiUrl.MerchantUrl + '/findAuthenticationStatusById', {id: data.id}).subscribe(res => {
          if (res.status == 0) {
              this.merchantList = res.data;
              this.authenticationStatus = true;
              this.nzLoading = false;
          }else{
              this.nzLoading = false;
              this.baseService.layer.messageError(res.msg);
          }
        });
    }
  }

  getAllAgencyOfName() {
    this.baseService.get(ApiUrl.AgencyUrl + '/getAllAgencyOfName').subscribe(data => {
      if (data.status == 0) {
        this.agencyList = data.data;
      } else {
        this.baseService.layer.messageError(data.msg);
      }
    });
  }

  gainAgencyId($event: any) {
    this.getAllUserOfName();
  }

  getAllUserOfName() {
    let agencyId = this.formModel.get("agencyId").value;
    if (agencyId == null) {
      agencyId = '0';
    }
    this.baseService.get(ApiUrl.UserUrl + '/getAllUserOfName?agencyId=' + agencyId).subscribe(data => {
      if (data.status == 0) {
        this.userList = data.data;
      } else {
        this.baseService.layer.messageError(data.msg);
      }
    });
  }

  gainUserId($event: any) {
    this.findAllMerchantStoreByUserInfo();
  }

  findAllMerchantStoreByUserInfo() {
    let userId = null;
    if (this.formModel.get("userId").value != null) {
      userId = this.formModel.get("userId").value
    } else {
      userId = '';
    }
    this.baseService.get(ApiUrl.MerchantStoreUrl + '/findAllMerchantStoreByUserInfo?userId=' + userId).subscribe(
      data => {
        if (data.status == 0) {
          this.merchantStoreList = data.data;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

}
